﻿var shown = 1;  //Y Position
var index_old = 0; //X Position
var slides_height = 454;
var scrollSpeed = 1000;

$(function () {
    /*Sizes fixing*/
    $('#gallery').css('width', $('.body').width() + "px");
    $('.slide').css('width', $('.body').width() + "px");
    $('#gallery').css('height', ($('#content').height() - $("#menu").height()) + "px"); //minus de header and the top
    $('#slides').css('height', $('#gallery').height() + "px");
    $('.slide').css('height', $('#slides').height() + "px");
    $('.slide2').css('height', $('#slides').height() + "px");

    marg_top = ($('#slides').height() - slides_height - $('.footer').height()) / 2;

    $('.footer').css('margin-top', marg_top + "px");
    $('.footer_upper').css('margin-top', "0px");
    /*\Sizes fixing*/


    /*Footer Hover*/
    $(".footer_hidden").hide();
    $(".footer_networking").click(function () {
        var src_ = $(this).attr("id");
        if ($(this).hasClass("twitter") == true) {
            window.open('http://www.twitter.com/arrb_group');
        } else if ($(this).hasClass("flicker") == true) {
            window.open('http://www.flickr.com/photos/ArrbGroup');
        } else if ($(this).hasClass("facebook") == true) {
            window.open('http://www.facebook.com/GroupArrb ');
        } else {
            alert("Blog!");
        }
    });

    $(".footer_networking").hover(
        function () {
            if (!$(this).hasClass("on_animation")) {
                var src_ = $(this).attr("id");
                var class_ = $("#menu .active a").attr("class");
                if (class_ == undefined) {
                    class_ = "blue";
                }
                if ($(this).hasClass("twitter") == true) {
                    $(".twitter_2").css("background-image", "url(" + $('#contentFolder').val() + "/Images/footer_hover/" + class_ + "/twitter.png)");
                    $(".twitter_1").hide();
                    $(".twitter_2").show();
                } else if ($(this).hasClass("flicker") == true) {
                    $(".flicker_2").css("background-image", "url(" + $('#contentFolder').val() + "/Images/footer_hover/" + class_ + "/flicker.png)");
                    $(".flicker_1").hide();
                    $(".flicker_2").show();
                } else if ($(this).hasClass("facebook") == true) {
                    $(".facebook_2").css("background-image", "url(" + $('#contentFolder').val() + "/Images/footer_hover/" + class_ + "/facebook.png)");
                    $(".facebook_1").hide();
                    $(".facebook_2").show();
                } else {
                    $(".blog_2").css("background-image", "url(" + $('#contentFolder').val() + "/Images/footer_hover/" + class_ + "/blog.png)");
                    $(".blog_1").hide();
                    $(".blog_2").show();
                }
            }
        },
        function () {
            if (!$(this).hasClass("on_animation")) {
                var src_ = $(this).attr("id");
                if ($(this).hasClass("twitter") == true) {
                    $(".twitter_1").show();
                    $(".twitter_2").hide();
                } else if ($(this).hasClass("flicker") == true) {
                    $(".flicker_1").show();
                    $(".flicker_2").hide();
                } else if ($(this).hasClass("facebook") == true) {
                    $(".facebook_1").show();
                    $(".facebook_2").hide();
                } else {
                    $(".blog_1").show();
                    $(".blog_2").hide();
                }
            }
        }
    );
    /*/Footer Hover*/

    /*Slide Out Tab*/
    var totWidth = 0;
    var positions = new Array(); ;
    $(".slide_out_wrapper").css({ 'top': ($('.body').height() - $('.slide_out_wrapper').height()) / 2 + 'px' });
    $(".handle").css({ 'top': ($(".slide_out_wrapper").height() - $(".handle").height()) / 2 + 'px' });
    $(".handle").css({ 'left': ($(".handle_wrapper").width() - $(".handle").width()) / 2 + 'px' });

    $(".slide_out_wrapper").css({ 'right': '-' + parseInt($(".slide_out_wrapper").outerWidth(), 10) + 'px' });
    $(".handle").click(function (e) {
        e.preventDefault();
        if ($(".slide_out_wrapper").hasClass('open')) {
            $(".slide_out_wrapper").animate({ 'right': '-' + parseInt($(".slide_out_wrapper").outerWidth(), 10) + 'px' }, 300).removeClass('open');
        } else {
            $(".slide_out_wrapper").animate({ right: '0px' }, 300).addClass('open');
        }
    });
    /*\Slide Out Tab*/


    $('#slides .slide').each(function (i) {

        /* Traverse through all the slides and store their accumulative widths in totWidth */

        positions[i] = totWidth;
        totWidth += $(this).width();

        /* The positions array contains each slide's commulutative offset from the left part of the container */

        if (!$(this).width()) {
            alert("Please, fill in width & height for all your images!");
            return false;
        }

    });

    $('#slides').width(totWidth);

    $("#menu a").click(function (e) {
        e.preventDefault();
        /* Casos*/
        $('#caso_slide_tab_overlay').hide();
        index_old = $("#menu .active").index() + 1;
        /*Sliding*/
        if (!$(this).hasClass('waiting')) {

            $("#volver_nosotros").hide();

            $(".submenu li").removeClass("sub_active");
            var href = $('#contentFolder').val() + "/CSS/color_" + $(this).attr('class') + ".css";
            $("#menu .active").removeClass("active");
            $("." + $(this).attr('class')).parent().addClass("active");
            $("#css_color").attr("href", href);
            $.cookie("css", href, { expires: 365, path: '/' });

            var index_current = $("#menu .active").index() + 1;

            if (index_current != 6) {
                $(".slide_out_wrapper").hide();
            }

            $("#menu li a").addClass("waiting");
            $(".slide_out_wrapper").removeClass('open');
            if (shown != 1) {
                if (shown < 1) {
                    //Going Down and Horizontal if needed
                    var n_times = Math.abs(1 - shown);
                    goDown(n_times, index_old, index_current);
                } else {
                    //Going Up and Horizontal if needed
                    var n_times = Math.abs(1 - shown);
                    goUp(n_times, index_old, index_current);
                }
            } else {
                //Esta en la posicion Y = 0 [MAIN]
                if ($(this).hasClass('red')) {
                    //$("#div_nosotros").css('visibility', 'visible');
                } else {
                    //$(".submenu_wrapper").css('visibility', 'hidden');
                    //$("#div_nosotros").css('visibility', 'hidden');
                }
                horizontalSlide(index_old, index_current);
            }
        }

        $("#css_color").attr("href", href);
        $.cookie("css", href, { expires: 365, path: '/' });
        return false;
    });

    $("#menu li a").hover(
        function () {
            if ($(this).hasClass("not_main")) {
                return;
            }
            var href = $('#contentFolder').val() + "/CSS/color_" + $(this).attr('class').split(' waiting')[0] + ".css";
            $("#css_color").attr("href", href);
            $.cookie("css", href, { expires: 365, path: '/' });
        },
        function () {
            if ($(this).hasClass("not_main")) {
                return;
            }
            var active_ = $("#menu .active a").attr('class');
            if (!active_) {
                active_ = "blue";
            }
            var href = $('#contentFolder').val() + "/CSS/color_" + active_.split(' waiting')[0] + ".css";
            $("#css_color").attr("href", href);
            $.cookie("css", href, { expires: 365, path: '/' });
        }
    );

    $("#logo").click(
        function () {
            index_old = $("#menu .active").index() + 1;

            if (!$("#menu a").hasClass('waiting')) {
                var href = $('#contentFolder').val() + "/CSS/color_blue.css";
                $("#menu .active").removeClass("active");
                $("#css_color").attr("href", href);
                var index_current = 0;

                /*Sliding*/
                if (shown != 1) {
                    if (shown < 1) {
                        //Going Down and Horizontal if needed
                        var n_times = Math.abs(1 - shown);
                        goDown(n_times, index_old, index_current);
                    } else {
                        //Going Up and Horizontal if needed
                        var n_times = Math.abs(1 - shown);
                        goUp(n_times, index_old, index_current);
                    }
                } else {
                    //Esta en la posicion Y = 0 [MAIN]
                    horizontalSlide(index_old, index_current);
                }
                /*/Sliding*/

                $(this).attr('src', $('#contentFolder').val() + '/Images/logo.png');
            }
        }
    );

    $("#logo").hover(
        function () {
            if ($("#menu .active a").attr('class')) {
                $(this).attr('src', $('#contentFolder').val() + '/Images/to_home.png');
            }
        },
        function () {
            if ($("#menu .active a").attr('class')) {
                $(this).attr('src', $('#contentFolder').val() + '/Images/logo.png');
            }
        }
    );

    /*Setting the home slide*/
    //$('.slide').find('.slide2:eq(' + shown + ')').css('margin-top', '-' + $('.slide2').height() + 'px');
    $(".slide_0").css('margin-top', '-' + $('.slide2').height() + 'px');
    $(".slide_1").css('margin-top', (($('#slides').height() - slides_height - $('.footer').height()) / 2) + "px");
    $(".slide_2").css('margin-top', (($('#slides').height() - slides_height - $('.footer').height()) / 2) + "px");
    //$('.slide').find('.slide2:eq(2)').css('margin-top', $('.slide2').height() + 'px');
    //$('.slide').find('.slide2:eq(' + (shown + 1) + ')').css('margin-top', '0px');
    /*/Setting the home slide*/


    /*Init*/
    $("#menu .active").removeClass("active");
    $("#volver_nosotros").hide();
    $(".slide_out_wrapper").hide();
    $("#css_color").attr("href", $('#contentFolder').val() + "/CSS/color_blue.css");
    //$.cookie("css", "Content/CSS/color_blue.css", { expires: 365, path: '/' });
    /*/Init*/
});

function goUp(times, index_old, index_current) {
    /*Block Footer HOVER*/
    $(".footer_networking").addClass("on_animation");
    /*\Block Footer HOVER*/

    if (index_old == 1 && shown == 2) {
        $(".slide_out_wrapper2").hide();
        offset = $("#caso_imagen").position().left - ($(".slide_out_wrapper2").position().left);
        right_ = parseFloat($(".slide_out_wrapper2").css('right').split("px")[0]);
        $(".slide_out_wrapper2").animate({ 'right': right_ + 468 + 'px' }, 1000).removeClass('open');
        $(".slide_out_wrapper2").css('right', '0px');
    }

    var total = parseFloat($('.slide_0').css('margin-top').split('px')[0]) + $('.slide_2').height() + parseFloat($('.slide_1').css('margin-top').split('px')[0]);
    shown = shown - times;
    $('.slide_0').stop().animate({ queue: true, marginTop: total + 'px' }, (scrollSpeed), function () {
        //$('.slide_' + (shown + 1))
        //$('.slide_' + (shown + 2))
        horizontalSlide(index_old, index_current);
        $("#menu li a").removeClass("waiting");
        if (index_current != 2) {
            $("#volver_nosotros").fadeOut('slow'); ;
        }
        if (index_current == 2 && shown == 0) {
            $("#volver_nosotros").css('visibility', 'visible');
            $("#volver_nosotros").fadeIn('slow');
        }
        $(".footer_networking").removeClass("on_animation");
    });
        
}


function goDown(times, index_old, index_current) {
    /*Block Footer HOVER*/
    $(".footer_networking").addClass("on_animation");
    /*\Block Footer HOVER*/

    //var total = parseFloat($('.column' + index_old).css('margin-top').split('px')[0]) - ($('.slide2').height() * (times));
    //total -= (($('#slides').height() - 454 - $('.footer').height()) / 2);
    var total = parseFloat($('.slide_' + (shown)).css('margin-top').split('px')[0]) + $('.slide_' + (shown)).height();
    if (shown == 0) {
        total -= parseFloat($('.slide_' + (shown + 1)).css('margin-top').split('px')[0]);
    } else {
        total -= parseFloat($('.slide_' + (shown - 1)).css('margin-top').split('px')[0]);
    }
    $("#volver_nosotros").fadeOut('slow');

    shown = shown + times;
    $('.slide_0').stop().animate({ queue: true, marginTop: -total + 'px' }, scrollSpeed, function () {
        horizontalSlide(index_old, index_current);
        $("#menu li a").removeClass("waiting");
        if (index_current != 1) {
            $(".slide_out_wrapper2").hide();
        }
        if (index_current == 1 && shown == 2) {
            $(".slide_out_wrapper2").fadeIn('slow');
        }
        $(".footer_networking").removeClass("on_animation");
    });

}

function horizontalSlide(index_old, index_current) {
    
    /*Block Footer HOVER*/
    $(".footer_networking").addClass("on_animation");
    /*\Block Footer HOVER*/

    var n_times = 0; 
    if (index_current != 5) {
        $(".slide_out_wrapper").hide();
    }
    if (index_old < index_current) {
        //Derecha
        n_times = index_current - index_old;        
        var total = Math.abs(parseFloat($('#slides').css('margin-left').split('px')[0])) + ($('.slide').width() * n_times);
        $('#slides').stop().animate({ queue: true, marginLeft: -total + 'px' }, scrollSpeed, function () {
            $("#menu li a").removeClass("waiting");
            $(".submenu li a").removeClass("waiting");
            shown = 1;
            if (index_current == 6) {
                $(".slide_out_wrapper").fadeIn('slow');
            }
            $(".footer_networking").removeClass("on_animation");

        });   //A LA DERECHA
    } else if (index_old > index_current) {
        //Izq
        n_times = index_old - index_current;
        var total = parseFloat($('#slides').css('margin-left').split('px')[0]) + ($('.slide').width() * n_times);
        $('#slides').stop().animate({ queue: true, marginLeft: total + 'px' }, scrollSpeed, function () {
            $("#menu li a").removeClass("waiting");
            $(".submenu li a").removeClass("waiting");
            shown = 1;
            if (index_current == 6) {
                $(".slide_out_wrapper").fadeIn('slow');
            }
            $(".footer_networking").removeClass("on_animation");
        });   //A LA IZQ
    }
}